/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Endpoint.h"

namespace panda::es2panda::ls {
class Es2pandaLSPServer : public Endpoint::MessageHandler {
private:
    using CallBack = std::function<void(std::optional<JsonObjectBuilder> &&value)>;
    using NotifyHandler = std::function<void(const JsonObject &params)>;
    using MethodHandler = std::function<void(const JsonObject &params, CallBack &&callback)>;

public:
    Es2pandaLSPServer(Endpoint &ep);
    ~Es2pandaLSPServer() = default;

    bool Run();
    
    // handlers
    void onInitialize(const JsonObject &params, CallBack &&cb);
    void onCompletion(const JsonObject &params, CallBack &&cb);
    // override
    bool onCall(const std::string &method, panda::JsonObject &params, panda::JsonObject::NumT id);
private:
    os::memory::Mutex method_handlers_mutex_;
    void OnCall(const char *method, MethodHandler &&handler);
    void OnNotify(const char *method, NotifyHandler &&handler);

    std::unordered_map<std::string, NotifyHandler> notify_handlers_ GUARDED_BY(method_handlers_mutex_);
    std::unordered_map<std::string, MethodHandler> method_handlers_ GUARDED_BY(method_handlers_mutex_);

    Endpoint &ep_;
};
}
